package com.superatom.designpattern.singleton.lazysingleton;

/**
 * 单例模式-懒汉模式-静态内部类实现-此方法线程安全
 */
public class LazyStaticSingleton {

    private static boolean initialized = false;

    private LazyStaticSingleton(){
        synchronized (LazyStaticSingleton.class){
            if (initialized == false){
                initialized = ! initialized;
            }else{
                throw new RuntimeException("单例已被侵犯");
            }
        }
    }

    // 内部类，利用了当外部类调用内部类时，才创建内部类的特点。线程安全
    private static class LazyStaticSingletonHandle{
        private static LazyStaticSingleton INSTANCE = new LazyStaticSingleton();
    }

    public static final LazyStaticSingleton getInstance(){
        return LazyStaticSingletonHandle.INSTANCE;
    }
}
